package cn.lic.tool.poi.excel.function.impl;

import org.apache.poi.ss.usermodel.Cell;
import com.alibaba.fastjson.JSONObject;
import cn.lic.tool.poi.excel.ParserUtils;
import cn.lic.tool.poi.excel.function.AbstractFunction;
import cn.lic.tool.poi.excel.function.IFunction;
import cn.lic.tool.poi.excel.function.IFunctionArgument;
import cn.lic.tool.poi.excel.function.IValue;
import cn.lic.tool.poi.excel.function.Range;
import cn.lic.tool.poi.excel.function.IValue.Type;

/**
 * 将参数转换为引用变量,
 * 不论传入的参数是直接量还是引用变量，均做引用变量解析
 * @author T_lichuang1_kzx
 *
 */
public class Reference extends AbstractFunction {
	
	public Reference() {}
	
	public Reference(IFunctionArgument[] args) {
		this.args = args;
	}

	@Override
	public IFunction build(IFunctionArgument[] args) {
		return new Reference(args);
	}
	@Override
	public IValue execute(JSONObject ctx) {
		String key = this.getArg(0, ctx).castValue(Type.STRING);
		return IValue.buildSValue(key == null ? null : ctx.get(key));
	}

	@Override
	public Range execute(JSONObject ctx, Cell cell) {
		IValue rs = this.execute(ctx);
		ParserUtils.setCellValue(cell, rs);
		return null;
	}
}
